package com.schoolET.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.schoolET.entity.Goods;
import com.schoolET.entity.dto.GoodsInfoDTO;
import com.schoolET.entity.dto.GoodsDTO;
import com.schoolET.entity.vo.InquireGoodsVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * <p>
 * 商品信息 Mapper 接口
 * </p>
 *
 * @author lin
 * @since 2024-05-31
 */
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {

    Page<Goods> selectByPositionAndTypeAndTheme(Page<?> page,@Param("inquireGoodsVO")InquireGoodsVO inquireGoodsVO);

    @Select("SELECT g.*,r.position FROM goods g\n" +
            "\tINNER JOIN goods_and_region gr ON g.id=gr.goods_id\n" +
            "\tINNER JOIN region r ON gr.region_id=r.id\n" +
            "\tWHERE g.flag = 0\n" +
            "\tLIMIT #{pageNum},#{pageSize}")
    @Results({@Result(property = "goodsTypeList",many=@Many(select = "com.schoolET.mapper.GoodsAndTypeMapper.selectGoodsTypeByGoodsId"),column = "id"),
            @Result(property = "wechatInfo",column = "wechat_id",one=@One(select = "com.schoolET.mapper.WechatMapper.selectByOpenId")),
            @Result(property = "id",column = "id")
    })
    List<GoodsDTO> selectGoodsDTOPage(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize);

    @Select("select " +
            "t1.wechat_id,t1.address,t1.picture,t1.price,t1.theme,t1.goods_describe,t1.create_time,t3.type " +
            "from goods t1 " +
            "left join goods_and_type t2 on t1.id = t2.goods_id " +
            "left join goods_type t3 on t2.goods_type_id = t3.id " +
            "left join order_management t4 on t1.id = t4.goods_id " +
            "where t1.id = #{id} " +
            "and t1.flag = 0 " +
            "and t3.flag = 0 " +
            "and t4.trading_status = 1 " +
            "and t4.product_status = 0 " +
            "and t4.flag = 0")
    GoodsInfoDTO selectGoodsById(Integer id);

    @Insert("insert into goods_and_type(goods_id, goods_type_id) VALUE (#{goodsId},#{typeId})")
    Integer insertGoodAndType(@Param("goodsId") Integer goodsId, @Param("typeId") Integer typeId);

    @Select("select t1.* from \n" +
            "goods t1 left join order_management t2 \n" +
            "on t1.id = t2.goods_id\n" +
            "where buyer_id = #{id} and t2.trading_status = 0")
    Page<Goods> selectMyPurchaseGoods(Page<?> page, @Param("id")Integer id);

    @Select("select t1.* from \n" +
            "goods t1 left join order_management t2 \n" +
            "on t1.id = t2.goods_id\n" +
            "where release_id = #{id} and product_status = 0 and t2.trading_status = 1")
    Page<Goods> selectMySellGoods(Page<?> page, @Param("id")Integer id);


    Page<Goods> selectByGoodsTypeIdAndTheme(Page<?> page,
                                            @Param("goodsTypeId")Integer goodsTypeId,
                                            @Param("theme")String theme,
                                            @Param("regionId")Integer regionId,
                                            @Param("wechatId") Integer wechatId);


    @Insert("insert into goods_and_region(goods_id, region_id) values (#{goodsId},#{regionId})")
    Integer insertGoodsAndRegion(@Param("goodsId") Integer goodsId, @Param("regionId") Integer regionId);


    @Select("select " +
            "t1.* " +
            "from goods t1 " +
            "left join goods_and_type t2 on t1.id = t2.goods_id " +
            "left join goods_type t3 on t2.goods_type_id = t3.id " +
            "left join order_management t4 on t1.id = t4.goods_id " +
            "where t1.id = #{id} " +
            "and t1.flag = 0 " +
            "and t3.flag = 0 " +
            "and t4.trading_status = 0 " +
            "and t4.product_status = 0 " +
            "and t4.flag = 0")
    Goods selectGoodsByConfirmId(Integer id);

}
